<?php

namespace App\Http\Controllers\Test;

use App\Models\Article;
use App\Http\Controllers\Controller;
use App\Models\News;


class SqlController extends Controller
{
    // sql测试
    public function initialize()
    {
        parent::initialize();
    }

    //  搜索 like or
    public function search()
    {
        $model = new Article();
        $where = [];

        // 构建查询条件
        $keyword = '哈哈';
        $channel = 'news';
        $cate_id = 8;
        if($cate_id){
            $where[] = ['cate_id', '=', $cate_id];
        }
        if($channel){
            $where[] = ['channel', '=', $channel];
        }
        if($keyword){
            /*$where[] = [
                function($query) use ($keyword) {
                    // 失败 同where
                    //$query->orWhere([['title', 'like', "%$keyword%"], ['describe', 'like', "%$keyword%"]]);
                    // 成功
                    $query->orWhere('title', 'like', "%$keyword%")->orWhere('describe', 'like', "%$keyword%");
                },
            ];*/

            // 成功
            $where[] = $this->orWhere('title|describe', 'like', $keyword);
        }


        /*if($time){
            $where[] = [function($query) use ($time) {
                $query->orWhere([['ctime', '=', 0], ['ctime', '<', $time]]);
            },];
        }*/

        $author_id = 0;
        if($author_id){
            $where[] = ['author_id', '=', $author_id];
        } else{
            $where[] = ['author_id', '>', 0];
        }


        $lists  = $model->listPage($where);
        $sqls   = $model->getSql();
        $result = ['sql' => $sqls, 'info' => $lists,];

        return $this->success($result);
    }

    // in
    public function in()
    {
        $lists = (new News())->where('channel', 'in', ['news', 'long'])->limit(10)->get();

        return $this->success($lists);
    }

}


